# 运算符:


# 1. 算数运算符: +  -  *  /  %  **(乘方)  //
result = 1 + 2 * 3
print(f"1 + 2 * 3 = {result}")
# 在python中,整数相除,除不尽得到的是小数,不会截断.
result = 1 / 2  # 结果不是 0!!! 和 JAVA是不一样的!! 同时0不能作被除数
print(f"1 / 2  = {result}")
result = 1 // 2  # 向下取整.
print(f"1 // 2  = {result}")
result = -1 // 2  # 向下取整.
print(f"-1 // 2  = {result}")

# **表示乘方, 此处表示4的3次方
result = 4 ** 3
print(f"4 ** 3 = {result}")
print(f"result ^ 0.5 = {result ** 0.5}")

# % 求余数  余数是一定小于除数的
result = 4 % 3
print(f"4 % 3 = {result}")
result = 4 % 3.0
print(f"4 % 3.0 = {result}")
result = 4 % -3
print(f"4 % -3 = {result}")
result = -4 % 3
print(f"-4 % 3 = {result}")
print("==========================================")


# 2. 关系运算符: >  <  <=  >=  ==
a = 30
b = 20
print(a > b)
print(a >= b)
print(a < b)
print(a <= b)
print(a == b)
print(a != b)

print("==========================================")

a = "hello world"
b = "Hello world"
print(a > b)
print(a >= b)
print(a < b)
print(a <= b)
print(a == b)
print(a != b)
print("==========================================")


# 对于浮点数来说 使用 == 来比较相等 存在一定的风险.
print(f"0.1 + 0.2 == 0.3 ? {0.1 + 0.2 == 0.3}")  # False
print(f"0.1 + 0.2 = {0.1 + 0.2}")
# 正确的做法是做差 看差值是否在误差范围内.
a = 0.1 + 0.2
b = 0.3
# python 支持这种连续的比较. 相当于java中: ((a-b) > -0.000001 && (a-b) < 0.000001)
print(-0.000001 < (a - b) < 0.000001)
print(((a - b) > -0.000001 and (a - b) < 0.000001))
print("==========================================")


# 3. 逻辑运算符: and(并且)  or(或者)  not(逻辑取反)  ==> 对应java中的: &&  ||  !
a = 10
b = 20
c = 30
print(a < b and b < c)
print(a < b and b > c)

print(a > b or b < c)
print(a < b or b > c)
print(a > b or b > c)

print(not a < b)
print(not a > b)
print("==========================================")

# 4. 赋值运算符: =  +=  -=  *=  %=  /=  //= (用法和java相同).
a = b = 20
print(a, b)
a, b = 10, 20
print(a, b)

# 交换两个数的值:
c = 10
d = 20
print(f"交换前: c, d = {c, d}")
c, d = d, c  # 两个赋值操作同时进行
print(f"交换后: c, d = {c, d}")
# ++a  a++ 在python 中适用嘛??? 不支持!! ---> ++, -- 这样的自增自自减操作是不支持的!!!

# 身份运算符: is, is not   成员元素算符: in, not in   位运算符: &, |, ^, <<, >>, 等不介绍 自行了解.

